package work.chief.servlet;

import work.chief.entity.Category;
import work.chief.entity.Product;
import work.chief.service.CategoryService;
import work.chief.service.ProductService;
import work.chief.service.impl.CategoryServiceImpl;
import work.chief.service.impl.ProductServiceImpl;
import work.chief.utils.PageUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet(name = "productServlet",urlPatterns = "/productServlet")
public class ProductServlet extends BaseServlet {
    private ProductService productService = new ProductServiceImpl();
    private CategoryService categoryService=new CategoryServiceImpl();

    //重写service方法showByPid
    public void showByPid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端传递的参数
        String pid=req.getParameter("pid");
        System.out.println("选中商品"+pid);
        Product productDetail =productService.selectByPid(Integer.parseInt(pid));

        //将对象存入作用域,
        req.setAttribute("productDetail",productDetail);
        req.getRequestDispatcher("product_detail.jsp").forward(req,resp);
        System.out.println("商品详情"+pid);
    }

    public void showProduct(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        String name = request.getParameter("pname");
//        System.out.println("搜索名称"+name);
        String stateStr = request.getParameter("state");
        String startTime = request.getParameter("start_time");
        String endTime = request.getParameter("end_time");
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");

        Integer currentPage=null;
        if (currentPageStr==null||"".equals(currentPageStr)){
            currentPage=1;
        }else {
            currentPage=Integer.parseInt(currentPageStr);
        }
        Integer pageSize=null;
        if (pageSizeStr==null||"".equals(pageSizeStr)){
            pageSize=10;
        }else {
            pageSize=Integer.parseInt(pageSizeStr);
        }
        Integer state =null;
        if (stateStr==null||"".equals(stateStr)){
            state=null;
        }else {
            state=Integer.parseInt(stateStr);

        }
        List<Product> selectProductPages = productService.selectProductPage(name, state, startTime, endTime,currentPage,pageSize);
        Integer pageCount = productService.selectProductCount(name, state, startTime, endTime);

        //总页数
        Integer totalPageSize=pageCount%pageSize==0 ? pageCount/pageSize : pageCount/pageSize+1;

        PageUtils pageUtils=new PageUtils();
        pageUtils.setTotalPageCount(pageCount);
        pageUtils.setCurrentPage(currentPage);
        pageUtils.setPageSize(pageSize);
        pageUtils.setTotalPageSize(totalPageSize);


        request.setAttribute("selectProductPages",selectProductPages);
        request.setAttribute("pageUtils",pageUtils);

        request.setAttribute("name",name);
        request.setAttribute("state",state);
        request.setAttribute("startTime",startTime);
        request.setAttribute("endTime",endTime);
        request.getRequestDispatcher("admin/product_list.jsp").forward(request,response);
        //
//        System.out.println("跳转成功");

    }
    public void deleteProduct(HttpServletRequest request,HttpServletResponse response) throws IOException {
        PrintWriter pw=response.getWriter();
        String id = request.getParameter("id");
        int i = productService.deleteByPid(id);
        if (i>0){
            pw.print(true);
        }else {
            pw.print(false);
        }
        pw.close();
    }
    public void showUpdateProduct(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String pid = request.getParameter("pid");
        System.out.println("pid::"+pid);
        Product product = productService.selectByPid(Integer.parseInt(pid));

//        System.out.println("pid::"+pid);
//        System.out.println(product);

        //查找14条分类记录
        List<Category> categoryList = categoryService.selectCategory(14);

        request.setAttribute("product",product);
        request.setAttribute("categoryList",categoryList);
        request.getRequestDispatcher("admin/product_update.jsp").forward(request,response);
    }
    public Product getProduct(HttpServletRequest request,HttpServletResponse response) throws ParseException {
//        String pid = request.getParameter("pid");
        String cid = request.getParameter("cid");
        String name = request.getParameter("name");
        String color = request.getParameter("color");
        String price = request.getParameter("price");
        String description = request.getParameter("description");
        String pic = request.getParameter("pic");
        String state = request.getParameter("state");
        String version = request.getParameter("version");
        String product_date = request.getParameter("product_date");
        System.out.println("图片"+pic);
        Product product= new Product();
//        product.setPid(Integer.parseInt(pid));
        product.setCid(Integer.parseInt(cid));
        product.setName(name);
        product.setColor(color);
        product.setPrice(price);
        product.setDescription(description);
        product.setPic(pic);
        product.setState(Integer.parseInt(state));
        product.setVersion(version);
        product.setProduct_date(new SimpleDateFormat("yyyy-MM-dd").parse(product_date));

        return product;

    }
    public void updateProduct(HttpServletRequest request,HttpServletResponse response) throws ParseException, IOException {
        Product product=getProduct(request,response);
        String pid = request.getParameter("pid");
        product.setPid(Integer.parseInt(pid));
        int i = productService.updateProduct(product);
        request.setAttribute("msg",i);

        response.sendRedirect("productServlet?mark=showProduct");
    }
    public void goAddProduct(HttpServletRequest request,HttpServletResponse response) throws ParseException, IOException, ServletException {
        List<Category> categoryList = categoryService.selectCategory(14);
        request.setAttribute("categoryList",categoryList);
        request.getRequestDispatcher("admin/product_add.jsp").forward(request,response);

    }
    public void addProduct(HttpServletRequest request,HttpServletResponse response) throws ParseException, IOException {
        Product product=getProduct(request,response);
        product.setPic("show.jpg");

        int i = productService.addProduct(product);
//        System.out.println("添加结果"+i);
        response.sendRedirect("productServlet?mark=showProduct");
    }


}
